#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/12/13 19:10
# @Author  : yingxiao zhang
# @File    : get_stocks_data.py
import time

import pandas as pd
import tushare as ts

stock_list = pd.read_csv('data/stock_list.csv')
stock_list = stock_list[stock_list['list_date'] < 20211231]
stock_list = stock_list['ts_code'].tolist()
stock_list = [item for item in stock_list if not item.endswith('.BJ')]
print(len(stock_list))

# 使用自己的token，到官网申请
token = ''
ts.set_token(token)
pro = ts.pro_api()

for i in range(2370, len(stock_list)):
    stock = stock_list[i]
    print("data pieces:", i)
    #取000001的前复权行情
    df = ts.pro_bar(ts_code=stock, adj='qfq', start_date='20100101', end_date='20250217')
    df.to_csv(f'stocks_data_new/{stock}.csv')
    time.sleep(0.015)
    print(stock)
